package com.song.flowable.controller.process;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.song.flowable.entity.process.ProcessCategory;
import com.song.flowable.result.ResultEntity;
import com.song.flowable.service.process.ProcessCategoryService;
import com.song.flowable.view.vo.ProcessCategoryTree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 流程分类 前端控制器
 * </p>
 *
 * @author song
 * @since 2025-01-09
 */
@Api(tags = "流程分类 控制器")
@RestController
@RequestMapping("//process-category")
public class ProcessCategoryController {
    @Autowired
    private ProcessCategoryService processCategoryService;

    @ApiOperation(value = "获取流程分类树")
    @PostMapping("/tree")
    public ResultEntity tree(){
        LambdaQueryWrapper<ProcessCategory> lambda = new QueryWrapper<ProcessCategory>().lambda();
        lambda.isNull(ProcessCategory::getParentId).or().eq(ProcessCategory::getParentId, "");
        List<ProcessCategory> list = processCategoryService.list(lambda);
        List<ProcessCategoryTree> processCategoryTrees = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)){
            processCategoryTrees = fillChildren(list);
        }
        return ResultEntity.success(processCategoryTrees);
    }

    @ApiOperation(value = "新增流程分类")
    @PostMapping("/add")
    public ResultEntity add(@RequestBody ProcessCategory processCategory){
        if (checkExists(processCategory.getType())){
            return ResultEntity.failMessage("分类标识已存在");
        }
        processCategoryService.save(processCategory);
        return ResultEntity.success();
    }

    @ApiOperation(value = "修改流程分类")
    @PostMapping("/update")
    public ResultEntity update(@RequestBody ProcessCategory processCategory){
        processCategory.setType(null);
        processCategoryService.updateById(processCategory);
        return ResultEntity.success();
    }

    @ApiOperation(value = "删除流程分类")
    @PostMapping("/delete")
    public ResultEntity delete(@RequestParam String id){
        processCategoryService.removeById(id);
        return ResultEntity.success();
    }

    @ApiOperation(value = "查询流程分类详情")
    @GetMapping("/detail")
    public ResultEntity detail(@RequestParam String id){
        ProcessCategory processCategory = processCategoryService.getById(id);
        return ResultEntity.success(processCategory);
    }

    public boolean checkExists(String type){
        LambdaQueryWrapper<ProcessCategory> lambda = new QueryWrapper<ProcessCategory>().lambda();
        lambda.eq(ProcessCategory::getType, type);
        ProcessCategory processCategory = processCategoryService.getOne(lambda);
        return Optional.ofNullable(processCategory).isPresent();
    }

    public List<ProcessCategoryTree> fillChildren(List<ProcessCategory> list){
        List<ProcessCategoryTree> processCategoryTrees = new ArrayList<>();
        for (ProcessCategory processCategory : list) {
            ProcessCategoryTree processCategoryTree = new ProcessCategoryTree();
            processCategoryTree.setId(processCategory.getId())
                    .setType(processCategory.getType())
                    .setName(processCategory.getName());
            LambdaQueryWrapper<ProcessCategory> lambda = new QueryWrapper<ProcessCategory>().lambda();
            lambda.eq(ProcessCategory::getParentId, processCategory.getId());
            List<ProcessCategory> children = processCategoryService.list(lambda);
            if (CollectionUtil.isNotEmpty(children)){
                List<ProcessCategoryTree> childrenTree = fillChildren(children);
                processCategoryTree.setChildren(childrenTree);
            }
            processCategoryTrees.add(processCategoryTree);
        }
        return processCategoryTrees;
    }
}
